﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;
using System.Diagnostics.Contracts;

public partial class Shop : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = Request.QueryString["n"];
    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (User.Identity.IsAuthenticated)
        {
            //已登录用户，则从数据库取数据看到底该店是不是他喜欢的。
            var connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"];
            if (connString == null)
                throw new ArgumentNullException("无法获取指定的数据库连接字符串。");

            using (SqlConnection connection = new SqlConnection(connString.ConnectionString))
            {
                connection.Open();

                string userId = Membership.GetUser().ProviderUserKey.ToString();
                SqlCommand command = new SqlCommand(
                    string.Format("select * from [favorite shop] where [UserId]='{0}' and shopName='{1}'", userId, Request.QueryString["n"]),
                    connection);
                command.CommandType = System.Data.CommandType.Text;
                object obj = command.ExecuteScalar();

                if (obj == null)
                    AddLikeButton.Visible = true;
                else
                    RemoveLikeButton.Visible = true;
            }
        }
        else
            AddLikeButton.Visible = true;
    }
    void IdentityCheck()
    {
        if (User.Identity.IsAuthenticated == false)
        {
            Response.Redirect("login.aspx?returnUrl=" + ResolveUrl("~/shop.aspx?n=")+ n);
        }
    }
    protected void chatButton_Click(object sender, EventArgs e)
    {
        IdentityCheck();
    }
    protected void AddLikeButton_Click(object sender, EventArgs e)
    {
        IdentityCheck();

        var connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"];
        if (connString == null)
            throw new ArgumentNullException("无法获取指定的数据库连接字符串。");

        using (SqlConnection connection = new SqlConnection(connString.ConnectionString))
        {
            connection.Open();
            string userId = Membership.GetUser().ProviderUserKey.ToString();
            
            SqlCommand command = new SqlCommand(
                string.Format("insert into [favorite shop] values ('{0}','{1}')",userId,Request.QueryString["n"]),
                connection);
            command.CommandType = System.Data.CommandType.Text;
            int affectedRows = command.ExecuteNonQuery();
            Contract.Assert(affectedRows > 0, "数据库操作失败，该店没有加入到数据库表中。请稍候再试。");

            
        }
    }
    protected void RemoveLikeButton_Click(object sender, EventArgs e)
    {
        IdentityCheck();

        var connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"];
        if (connString == null)
            throw new ArgumentNullException("无法获取指定的数据库连接字符串。");

        using (SqlConnection connection = new SqlConnection(connString.ConnectionString))
        {
            connection.Open();
            string userId = Membership.GetUser().ProviderUserKey.ToString();

            SqlCommand command = new SqlCommand(
                string.Format("delete from [favorite shop] where [UserId]='{0}' and shopName='{1}'", userId, Request.QueryString["n"]),
                connection);
            command.CommandType = System.Data.CommandType.Text;
            int affectedRows = command.ExecuteNonQuery();
            Contract.Assert(affectedRows > 0, "数据库操作失败，该商品没有从数据库表中删除。请稍候再试。");
        }
    }

    public string n { get; set; }
}